﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using UnityEditorInternal;
    using UnityEngine;

    [CustomEditor(typeof(AvatarSkeletonMask))]
    internal class AvatarSkeletonMaskInspector : Editor
    {
        [CompilerGenerated]
        private static Func<char, bool> <>f__am$cache4;
        [CompilerGenerated]
        private static Func<char, bool> <>f__am$cache5;
        private bool m_CanImport = true;
        private NodeInfo[] m_NodeInfos;
        private Avatar m_RefAvatar;
        private ModelImporter m_RefImporter;

        private void CheckChildren(AvatarSkeletonMask mask, int index, float value)
        {
            foreach (int num in this.m_NodeInfos[index].m_ChildIndices)
            {
                mask.SetWeight(num, value);
                this.CheckChildren(mask, num, value);
            }
        }

        private void ComputeShownElements()
        {
            for (int i = 0; i < this.m_NodeInfos.Length; i++)
            {
                if (this.m_NodeInfos[i].m_ParentIndex == -1)
                {
                    this.ComputeShownElements(i, true);
                }
            }
        }

        private void ComputeShownElements(int currentIndex, bool show)
        {
            this.m_NodeInfos[currentIndex].m_Show = show;
            bool flag = show && this.m_NodeInfos[currentIndex].m_Expanded;
            foreach (int num in this.m_NodeInfos[currentIndex].m_ChildIndices)
            {
                this.ComputeShownElements(num, flag);
            }
        }

        private void FillNodeInfos(AvatarSkeletonMask mask)
        {
            this.m_NodeInfos = new NodeInfo[mask.count];
            for (int i = 1; i < this.m_NodeInfos.Length; i++)
            {
                this.m_NodeInfos[i].m_Expanded = true;
                this.m_NodeInfos[i].m_ParentIndex = -1;
                this.m_NodeInfos[i].m_ChildIndices = new List<int>();
                string path = mask.GetPath(i);
                if (<>f__am$cache4 == null)
                {
                    <>f__am$cache4 = f => f == '/';
                }
                this.m_NodeInfos[i].m_Depth = (i != 0) ? Enumerable.Count<char>(path, <>f__am$cache4) : 0;
                string str2 = string.Empty;
                int length = path.LastIndexOf('/');
                if (length > 0)
                {
                    str2 = path.Substring(0, length);
                }
                int count = mask.count;
                for (int j = 0; j < count; j++)
                {
                    string str3 = mask.GetPath(j);
                    if ((str2 != string.Empty) && (str3 == str2))
                    {
                        this.m_NodeInfos[i].m_ParentIndex = j;
                    }
                    if (str3.StartsWith(path))
                    {
                        if (<>f__am$cache5 == null)
                        {
                            <>f__am$cache5 = f => f == '/';
                        }
                        if (Enumerable.Count<char>(str3, <>f__am$cache5) == (this.m_NodeInfos[i].m_Depth + 1))
                        {
                            this.m_NodeInfos[i].m_ChildIndices.Add(j);
                        }
                    }
                }
            }
        }

        private void OnEnable()
        {
        }

        public override void OnInspectorGUI()
        {
            AvatarSkeletonMask target = base.target as AvatarSkeletonMask;
            if (this.canImport)
            {
                EditorGUI.BeginChangeCheck();
                this.m_RefAvatar = EditorGUILayout.ObjectField("Use skeleton from", this.m_RefAvatar, typeof(Avatar), true, new GUILayoutOption[0]) as Avatar;
                if (EditorGUI.EndChangeCheck())
                {
                    this.m_RefImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(this.m_RefAvatar)) as ModelImporter;
                }
                if ((this.m_RefImporter != null) && GUILayout.Button("Import skeleton", new GUILayoutOption[0]))
                {
                    string[] transformPaths = this.m_RefImporter.transformPaths;
                    target.count = transformPaths.Length;
                    for (int j = 0; j < transformPaths.Length; j++)
                    {
                        target.SetPath(j, transformPaths[j]);
                        target.SetWeight(j, 1f);
                    }
                }
            }
            if ((this.m_NodeInfos == null) || (target.count != this.m_NodeInfos.Length))
            {
                this.FillNodeInfos(target);
            }
            this.ComputeShownElements();
            GUILayout.Space(1f);
            int indentLevel = EditorGUI.indentLevel;
            int count = target.count;
            for (int i = 1; i < count; i++)
            {
                if (this.m_NodeInfos[i].m_Show)
                {
                    char[] separator = new char[] { '/' };
                    string[] strArray2 = target.GetPath(i).Split(separator);
                    string content = strArray2[strArray2.Length - 1];
                    GUILayout.BeginHorizontal(new GUILayoutOption[0]);
                    EditorGUI.indentLevel = this.m_NodeInfos[i].m_Depth;
                    EditorGUI.BeginChangeCheck();
                    GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.ExpandWidth(false) };
                    Rect position = GUILayoutUtility.GetRect((float) 20f, (float) 15f, options);
                    position.x += 5f;
                    bool flag = Event.current.button == 1;
                    target.SetWeight(i, !GUI.Toggle(position, target.GetWeight(i) > 0.1, string.Empty) ? ((float) 0) : ((float) 1));
                    if (EditorGUI.EndChangeCheck() && !flag)
                    {
                        this.CheckChildren(target, i, target.GetWeight(i));
                    }
                    if (this.m_NodeInfos[i].m_ChildIndices.Count > 0)
                    {
                        this.m_NodeInfos[i].m_Expanded = EditorGUILayout.Foldout(this.m_NodeInfos[i].m_Expanded, content);
                    }
                    else
                    {
                        EditorGUILayout.LabelField(content, new GUILayoutOption[0]);
                    }
                    GUILayout.EndHorizontal();
                }
            }
            EditorGUI.indentLevel = indentLevel;
        }

        public bool canImport
        {
            get
            {
                return this.m_CanImport;
            }
            set
            {
                this.m_CanImport = value;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct NodeInfo
        {
            public bool m_Expanded;
            public bool m_Show;
            public int m_ParentIndex;
            public List<int> m_ChildIndices;
            public int m_Depth;
        }
    }
}

